using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace StockPresenter
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            Console.WriteLine("=== A股智能推荐系统 ===");
            Console.WriteLine("正在加载推荐数据...");

            string recommendationsFile = "../analyzer/recommendations.csv";
            
            if (!File.Exists(recommendationsFile))
            {
                Console.WriteLine("错误: 未找到推荐数据文件");
                Console.WriteLine("请先运行分析程序生成推荐数据");
                return;
            }

            var stocks = LoadRecommendations(recommendationsFile)
                .OrderByDescending(s => s.ChangePercent)
                .ToList();
            
            if (stocks.Count == 0)
            {
                Console.WriteLine("今日无符合推荐条件的股票");
                return;
            }

            // 计算统计信息
            var avgChange = stocks.Average(s => s.ChangePercent);
            var maxChange = stocks.Max(s => s.ChangePercent);
            var minChange = stocks.Min(s => s.ChangePercent);

            Console.WriteLine($"\n📈 今日推荐股票 ({stocks.Count}支)");
            Console.WriteLine("════════════════════════════════════");
            Console.WriteLine($"平均涨幅: {avgChange:F2}% | 最高: {maxChange:F2}% | 最低: {minChange:F2}%");
            Console.WriteLine("════════════════════════════════════");
            
            foreach (var stock in stocks)
            {
                // 根据涨幅设置颜色
                if (stock.ChangePercent > 3)
                    Console.ForegroundColor = ConsoleColor.Green;
                else if (stock.ChangePercent > 1)
                    Console.ForegroundColor = ConsoleColor.Yellow;
                else
                    Console.ForegroundColor = ConsoleColor.White;

                Console.WriteLine($"🔼 {stock.ChangePercent,5:F2}% | 💰 {stock.Price,6:F2}元 | 📊 {stock.Volume/10000,6:F0}万手 | K:{stock.KValue:F0} D:{stock.DValue:F0} J:{stock.JValue:F0} | MACD:{stock.DIF:F2}/{stock.DEA:F2}/{stock.MACD:F2} | {stock.Name}({stock.Code})");
                Console.ResetColor();
            }

            Console.WriteLine("\n🔍 推荐策略分析:");
            Console.WriteLine("- 涨幅区间: 1%~5%");
            Console.WriteLine("- 成交量: >10万手");
            Console.WriteLine("- 价格筛选: >10元");
            Console.WriteLine("- 技术指标: MACD金叉");
            Console.WriteLine("- 排序方式: 按涨幅从高到低");

            Console.WriteLine("\n输入任意键退出...");
            Console.ReadKey();
        }

        static List<Stock> LoadRecommendations(string filePath)
        {
            var stocks = new List<Stock>();
            
            try
            {
                var lines = File.ReadAllLines(filePath).Skip(1); // 跳过表头
                
                foreach (var line in lines)
                {
                    var parts = line.Split(',');
                    if (parts.Length >= 9)
                    {
                        stocks.Add(new Stock
                        {
                            Code = parts[0],
                            Name = parts[1],
                            Price = double.Parse(parts[2]),
                            ChangePercent = double.Parse(parts[3]),
                            Volume = double.Parse(parts[4]),
                            Date = parts[5],
                            KValue = parts.Length > 6 && !string.IsNullOrEmpty(parts[6]) ? double.Parse(parts[6]) : 50,
                            DValue = parts.Length > 7 && !string.IsNullOrEmpty(parts[7]) ? double.Parse(parts[7]) : 50,
                            JValue = parts.Length > 8 && !string.IsNullOrEmpty(parts[8]) ? double.Parse(parts[8]) : 50,
                            DIF = parts.Length > 9 && !string.IsNullOrEmpty(parts[9]) ? double.Parse(parts[9]) : 0,
                            DEA = parts.Length > 10 && !string.IsNullOrEmpty(parts[10]) ? double.Parse(parts[10]) : 0,
                            MACD = parts.Length > 11 && !string.IsNullOrEmpty(parts[11]) ? double.Parse(parts[11]) : 0
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"加载推荐数据出错: {ex.Message}");
            }

            return stocks;
        }
    }
}